//
//  XMButton.m
//  XMButton
//
//  Created by chenming on 2017/12/7.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "XMButton.h"

@interface XMButton ()
{
    CGFloat _btnOffset;
}
@end

@implementation XMButton

+ (instancetype)initWithFrame:(CGRect)frame buttonWithType:(UIButtonType)buttonType {
    return [[[self class] alloc] initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.backgroundColor = [UIColor yellowColor];
        self.imageView.backgroundColor = [UIColor orangeColor];
        self.offset = 20.f;
        self.font = [UIFont systemFontOfSize:14];
        self.titleColor = [UIColor redColor];
        [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)buttonClick:(UIButton *)btn {
    !_btnClick ? : _btnClick();
}

- (void)setHighlighted:(BOOL)highlighted {
    
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect bounds = self.bounds;
    //若原热区小于44x44，则放大热区，否则保持原大小不变
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}

- (void)setImageOrientationType:(XMButtonImageOrientationType)imageOrientationType {
    switch (imageOrientationType) {
        case XMButtonImageOrientationLeft:
        {
//            self.imageEdgeInsets = UIEdgeInsetsMake(0, -_btnOffset/2.0, 0, _btnOffset/2.0);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, _btnOffset / 2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, _btnOffset / 2, 0, -_btnOffset / 2);
        }
            break;
        case XMButtonImageOrientationRight:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width - self.frame.size.width + self.titleLabel.intrinsicContentSize.width, 0, 0);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -self.titleLabel.frame.size.width - self.frame.size.width + self.imageView.frame.size.width);
        }
            break;
        case XMButtonImageOrientationTop:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width, -self.imageView.frame.size.height - _btnOffset / 2, 0);
            // 由于iOS8中titleLabel的size为0，用上面这样设置有问题，修改一下即可
            self.imageEdgeInsets = UIEdgeInsetsMake(-self.titleLabel.intrinsicContentSize.height - _btnOffset / 2, 0, 0, -self.titleLabel.intrinsicContentSize.width);
        }
            break;
        case XMButtonImageOrientationBottom:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(-self.imageView.frame.size.width - _btnOffset / 2, -self.imageView.frame.size.width, 0, 0);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, -self.titleLabel.intrinsicContentSize.height - _btnOffset / 2, -self.titleLabel.intrinsicContentSize.width);
        }
            break;
        default:
            break;
    }
}

/*
-(void)layoutSubviews {
    [super layoutSubviews];
    
    switch (self.imageOrientationType) {
        case XMButtonImageOrientationTop:
        {
            [self setTitleEdgeInsets:UIEdgeInsetsZero];
            [self.titleLabel sizeToFit];
            CGRect labelFrame = self.titleLabel.frame;
            
            [self.imageView sizeToFit];
            CGRect imageFrame = self.imageView.frame;
            
            imageFrame.origin.x = (self.frame.size.width-imageFrame.size.width)*0.5;
            imageFrame.origin.y = (self.frame.size.height-imageFrame.size.height-labelFrame.size.height-_btnOffset)*0.5;
            self.imageView.frame = imageFrame;
            
            labelFrame.origin.x = (self.frame.size.width-labelFrame.size.width)*0.5;;
            labelFrame.origin.y = imageFrame.origin.y+imageFrame.size.height+_btnOffset;
            self.titleLabel.frame = labelFrame;
            
            break;
        }
        case XMButtonImageOrientationLeft:
        {
            [self.titleLabel sizeToFit];
            CGRect labelFrame = self.titleLabel.frame;
            [self.imageView sizeToFit];
            
            CGRect imageFrame = self.imageView.frame;
            imageFrame.origin.x = (self.frame.size.width-imageFrame.size.width-labelFrame.size.width-_btnOffset)*0.5;
            imageFrame.origin.y = (self.frame.size.height-imageFrame.size.height)*0.5;
            self.imageView.frame = imageFrame;
            
            labelFrame.origin.x = imageFrame.origin.x+imageFrame.size.width+_btnOffset;
            labelFrame.origin.y = (self.frame.size.height-labelFrame.size.height)*0.5;
            self.titleLabel.frame = labelFrame;
            break;
        }
        case XMButtonImageOrientationBottom:
        {
            [self.titleLabel sizeToFit];
            CGRect labelFrame = self.titleLabel.frame;
            
            [self.imageView sizeToFit];
            CGRect imageFrame = self.imageView.frame;
            
            labelFrame.origin.x = (self.frame.size.width-labelFrame.size.width)*0.5;
            labelFrame.origin.y = (self.frame.size.height-labelFrame.size.height-imageFrame.size.height-_btnOffset)*0.5;
            self.titleLabel.frame = labelFrame;
            
            imageFrame.origin.x = (self.frame.size.width-imageFrame.size.width)*0.5;
            imageFrame.origin.y = labelFrame.origin.y+labelFrame.size.height+_btnOffset;
            self.imageView.frame = imageFrame;
            
            break;
        }
        case XMButtonImageOrientationRight:
        {
            [self.titleLabel sizeToFit];
            CGRect labelFrame = self.titleLabel.frame;
            
            [self.imageView sizeToFit];
            CGRect imageFrame = self.imageView.frame;
            
            labelFrame.origin.x = (self.frame.size.width-imageFrame.size.width-labelFrame.size.width-_btnOffset)*0.5;
            labelFrame.origin.y = (self.frame.size.height-labelFrame.size.height)*0.5;
            self.titleLabel.frame = labelFrame;
            
            imageFrame.origin.x = labelFrame.origin.x+labelFrame.size.width+_btnOffset;
            imageFrame.origin.y = (self.frame.size.height-imageFrame.size.height)*0.5;
            self.imageView.frame = imageFrame;
            
            break;
        }
        default:
            break;
    }
}
*/

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    [self setTitleColor:_titleColor forState:UIControlStateNormal];
}

- (void)setFont:(UIFont *)font {
    _font = font;
    self.titleLabel.font = _font;
}

- (void)setOffset:(CGFloat)offset {
    _offset = offset;
    _btnOffset = _offset;
}

@end
